package com.cupk.inspection.repository;

import com.cupk.inspection.model.InspectionRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.sql.Timestamp;

public interface InspectionRecordRepository extends JpaRepository<InspectionRecord, Long> {

    /**
     * 根据设备 ID、开始时间和结束时间进行分页查询
     * @param deviceId 设备 ID
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param pageable 分页信息
     * @return 分页结果
     */
    @Query("SELECT ir FROM InspectionRecord ir " +
            "WHERE (:deviceId IS NULL OR ir.taskId = :deviceId) " +
            "AND (:startDate IS NULL OR ir.completionTime >= :startDate) " +
            "AND (:endDate IS NULL OR ir.completionTime <= :endDate)")
    Page<InspectionRecord> findByParams(@Param("deviceId") Long deviceId,
                                        @Param("startDate") Timestamp startDate,
                                        @Param("endDate") Timestamp endDate,
                                        Pageable pageable);
}